package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.my.util.MyUtil;

/**
 * 修改用户信息
 */
public class EditUserDataActivity extends SlidingActivity implements View.OnClickListener {

    public static final int RESULT_CODE = 0x08;

    public static final String EDITEXT_FLAG = "editText";//内容标识
    public static final String EDITTITLE_FLAG = "editTitle";//标题标识

    private EditText editText;
    private Intent intent;

    @Override
    protected int getResourceId() {
        return R.layout.activity_edit_user_data;
    }

    @Override
    protected void init() {
        intent = getIntent();

        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MyUtil.startKeyword(EditUserDataActivity.this, editText);
            }
        }, 300);
    }

    private void initView(){
        topName.setText(intent.getStringExtra(EDITTITLE_FLAG));
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
        rightBtn.setText("确定");
        rightBtn.setVisibility(View.VISIBLE);
        leftIcon.setOnClickListener(this);
        rightBtn.setOnClickListener(this);

        editText = (EditText) findViewById(R.id.edit_name);
        editText.setText(intent.getStringExtra(EDITEXT_FLAG));
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    onClick(rightBtn);
                }
                return true;
            }
        });
        editText.setSelection(editText.getText().toString().length());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                MyUtil.closeKeyword(this, editText);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 300);
                break;
            case R.id.right_bn:
                MyUtil.closeKeyword(this, editText);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        intent.putExtra(EDITEXT_FLAG, editText.getText().toString());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }, 300);
                break;
        }
    }
}
